package com.tsd.system.utils;

import com.tsd.base.entity.BaseRelatedPartyExt;
import com.tsd.core.utils.HlpUtils;
import com.tsd.system.entity.SysGroupExt;
import com.tsd.system.entity.SysResourceExt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TreeUtil
 * @Description: 列表组装树形数据
 * @Author: Luoyc
 * @Date: 2020/5/12 10:17
 * @Version: 1.0
 */
public class SysTreeUtil {

    public static List<SysResourceExt> expandResourceTree(List<SysResourceExt> resourceList) {
        if (HlpUtils.isEmptyList(resourceList)) {
            return resourceList;
        }
        List<SysResourceExt> resultList = new ArrayList<>();
        Map<String, SysResourceExt> resourceMap = new HashMap<>();
        for (SysResourceExt resource : resourceList) {
            resourceMap.put(resource.getSid(), resource);
        }
        for (SysResourceExt group : resourceList) {
            SysResourceExt hashVP = resourceMap.get(group.getParent_sid());
            if (hashVP != null) {
                List<SysResourceExt> childrens = hashVP.getChildrens();
                if (HlpUtils.isEmptyList(childrens)) {
                    childrens = new ArrayList<>();
                }
                childrens.add(group);
                hashVP.setChildrens(childrens);
            } else {
                resultList.add(group);
            }
        }
        return resultList;
    }

    public static List<SysGroupExt> expandSysGroupTree(List<SysGroupExt> groupList) {
        if (HlpUtils.isEmptyList(groupList)) {
            return groupList;
        }
        List<SysGroupExt> resultList = new ArrayList<>();
        Map<String, SysGroupExt> groupMap = new HashMap<>();
        for (SysGroupExt group : groupList) {
            groupMap.put(group.getSid(), group);
        }
        for (SysGroupExt group : groupList) {
            SysGroupExt hashVP = groupMap.get(group.getParent_sid());
            if (hashVP != null) {
                List<SysGroupExt> childrens = hashVP.getChildrens();
                if (HlpUtils.isEmptyList(childrens)) {
                    childrens = new ArrayList<>();
                }
                childrens.add(group);
                hashVP.setChildrens(childrens);
            } else {
                resultList.add(group);
            }
        }
        return resultList;
    }

    /**
     * 构造往来单位分类树
     *
     * @param list
     * @return
     */
    public static List<BaseRelatedPartyExt> expandRelatedPartyTypeTree(List<BaseRelatedPartyExt> list) {
        if (HlpUtils.isEmptyList(list)) {
            return list;
        }
        List<BaseRelatedPartyExt> resultList = new ArrayList<>();
        Map<String, BaseRelatedPartyExt> clientTypeMap = new HashMap<>();
        for (BaseRelatedPartyExt clientType : list) {
            clientTypeMap.put(clientType.getSid(), clientType);
        }
        for (BaseRelatedPartyExt clientType : list) {
            BaseRelatedPartyExt hashVP = clientTypeMap.get(clientType.getParent_sid());
            if (hashVP != null) {
                List<BaseRelatedPartyExt> childrens = hashVP.getChildren();
                if (HlpUtils.isEmptyList(childrens)) {
                    childrens = new ArrayList<>();
                }
                childrens.add(clientType);
                hashVP.setChildren(childrens);
            } else {
                resultList.add(clientType);
            }
        }
        return resultList;
    }

}
